package com.daisuxia.web.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daisuxia.baofoo.BaofooCutPayUtil;
import com.daisuxia.constant.Constant;
import com.daisuxia.payRoute.PayQueryRoute;
import com.daisuxia.risk.service.IOutOrdersService;
import com.daisuxia.web.dao.IOutOrdersDao;
import com.daisuxia.web.dao.IPaginationDao;
import com.daisuxia.web.dao.IRenewalRecordDao;
import com.daisuxia.web.pojo.*;
import com.daisuxia.web.util.DateUtil;
import com.daisuxia.web.util.PageConfig;


@Service
public  class RenewalRecordService implements IRenewalRecordService {

	private static Logger logger = Logger.getLogger(RenewalRecordService.class);

	@Autowired
	private IRenewalRecordDao renewalRecordDao;
    @Autowired
	private IPaginationDao paginationDao;
    @Autowired
	private IOutOrdersDao outOrdersDao;
    @Autowired
	private IRepaymentService repaymentService;

	@Override
	public RenewalRecord selectByPrimaryKey(Integer id) {
		return renewalRecordDao.selectByPrimaryKey(id);
	}

	@Override
	public boolean deleteByPrimaryKey(Integer id) {
		 return renewalRecordDao.deleteByPrimaryKey(id) > 0;
	}

	@Override
	public boolean insert(RenewalRecord repayment) {
		return renewalRecordDao.insert(repayment) > 0;
	}

	@Override
	public boolean insertSelective(RenewalRecord repayment) {
		return renewalRecordDao.insertSelective(repayment) > 0;
	}

	@Override
	public boolean updateByPrimaryKey(RenewalRecord repayment) {
		return renewalRecordDao.updateByPrimaryKey(repayment) > 0;
	}

	@Override
	public boolean updateByPrimaryKeySelective(RenewalRecord repayment) {
		return renewalRecordDao.updateByPrimaryKeySelective(repayment) > 0;
	}


	@Override
	public List<RenewalRecord> findParams(Map<String, Object> params) {
		return renewalRecordDao.findParams(params);
	}

	@Override
	public PageConfig<RenewalRecord> renewalList(HashMap<String, Object> params) {
		params.put(Constant.NAME_SPACE, "RenewalRecord");
		return paginationDao.findPage("renewalList", "renewalCount", params, "web");
	}

	@Override
	public PageConfig<RenewalRecord> findPage(HashMap<String, Object> params) {
		params.put(Constant.NAME_SPACE, "RenewalRecord");
		return paginationDao.findPage("findPage", "findPageCount", params, "web");
	}

	@Override
	public Integer renewalTotal(HashMap<String, Object> map) {
		// TODO Auto-generated method stub
		return renewalRecordDao.renewalTotal(map);
	}

	@Override
	public Long sumFeeTotal(HashMap<String, Object> map) {
		// TODO Auto-generated method stub
		return renewalRecordDao.sumFeeTotal(map);
	}

	@Override
	public Long renewalInterestTotal(HashMap<String, Object> map) {
		// TODO Auto-generated method stub
		return renewalRecordDao.renewalInterestTotal(map);
	}

	@Override
	public Long renewalFeeTotal(HashMap<String, Object> map) {
		// TODO Auto-generated method stub
		return renewalRecordDao.renewalFeeTotal(map);
	}

	@Override
	public Integer findPageCount(HashMap<String, Object> map) {
		// TODO Auto-generated method stub
		return renewalRecordDao.findPageCount(map);
	}
	@Override
	public List<RenewalRecord> queryRenewalOrder(){
		// TODO Auto-generated method stub
		return renewalRecordDao.queryRenewalOrder();
	}
	@Override
	public void dealRenewalOrder(RenewalRecord rr){
		try {
			// 查询订单的代扣方式
			OutOrders oo = outOrdersDao.findByOrderNo(rr.getOrderId());
			if(oo !=null){
				Map map = new HashMap();
				map.put("orderNo", oo.getOrderNo());
				map.put("payDate", DateUtil.getDateFormat(rr.getCreatedAt(), "yyyyMMddHHmmss"));
				map.put("payCutType", oo.getOrderType());

				Map resultMap = PayQueryRoute.queryTransPay(map);
				String outerType = resultMap.get("payCutType").toString();// 代扣方式
				String retCode = resultMap.get("resp_code").toString();// 返回码

				String signParam = resultMap.get("qbm_signParam").toString();// 请求参数
				String reqParam = resultMap.toString();// 返回参数

				logger.error(outerType + "_dealRenewalOrder:" + signParam);

				logger.error(outerType + "_dealRenewalOrder:" + reqParam);

				if ("0000".equals(retCode)) { // 请求成功
					if (OutOrders.TYPE_BAOFOO.equals(oo.getOrderType())) {
						//进入宝付
						setQueryOrderBF(oo, reqParam, rr, resultMap);
					} else if (OutOrders.TYPE_RONGBAO.equals(oo.getOrderType())) {
						//进入融宝
						setQueryOrderRB(oo, reqParam, rr, resultMap);
					}

				}

			}
		} catch (Exception e) {
			logger.error("dealRenewalOrder error......", e);
			e.printStackTrace();
		}
	}

	public void setQueryOrderBF(OutOrders oo, String reqParam, RenewalRecord rr, Map map) {
		String orderStat = map.get("order_stat").toString();// 支付状态码
		//状态是I FF 不作处理
		if (!BaofooCutPayUtil.queryCodes.contains(orderStat)) {
			oo.setReturnParams(reqParam);
			oo.setOrderNo(rr.getOrderId());
			if ("S".equals(orderStat)) {   // 支付成功
				oo.setStatus(OutOrders.STATUS_SUC);
				repaymentService.renewal(null, rr);
			} else if ("I".equals(orderStat)) {   //支付失败
				oo.setStatus(OutOrders.STATUS_OTHER);
			}
			outOrdersDao.updateByOrderNo(oo);
		}
	}
	public void setQueryOrderRB(OutOrders oo, String reqParam, RenewalRecord rr, Map map) {
		String string = map.get("batch_content").toString();
		String[] strings = string.split(",");
		String result_msg = strings[12];

		//状态是I FF 不作处理
		if (!result_msg.equals("处理中")) {
			String resultPay = "";
			oo.setReturnParams(reqParam);
			oo.setOrderNo(rr.getOrderId());

			if (result_msg.equals("成功")) {   // 支付成功
				oo.setStatus(OutOrders.STATUS_SUC);
				repaymentService.renewal(null, rr);
			} else if (result_msg.equals("失败")) {   //支付失败
				oo.setStatus(OutOrders.STATUS_OTHER);
			}
		}
		outOrdersDao.updateByOrderNo(oo);
	}

}
